package com.zjut.zs.userinfo.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zjut.an.util.module.PageModule;

/**
 * A data access object (DAO) providing persistence and search support for
 * ZsUserinfo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.zjut.zs.userinfo.dao.ZsUserinfo
 * @author MyEclipse Persistence Tools
 */

public class ZsUserinfoDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(ZsUserinfoDAO.class);
	// property constants
	public static final String IN_ID = "inId";
	public static final String IN_PINYING = "inPinying";
	public static final String IN_PHOTO = "inPhoto";
	public static final String IN_BYXXMC = "inByxxmc";
	public static final String IN_BYLB = "inBylb";
	public static final String IN_XB = "inXb";
	public static final String IN_MZ = "inMz";
	public static final String IN_KSLB = "inKslb";
	public static final String IN_ZZMM = "inZzmm";
	public static final String IN_ZJLX = "inZjlx";
	public static final String IN_ZJHM = "inZjhm";
	public static final String IN_HJMC = "inHjmc";
	public static final String IN_LXDH = "inLxdh";
	public static final String IN_LXDH2 = "inLxdh2";
	public static final String IN_KL = "inKl";
	public static final String IN_WYYZ = "inWyyz";
	public static final String IN_SDMC = "inSdmc";
	public static final String IN_SXMC = "inSxmc";
	public static final String IN_BMDMC = "inBmdmc";
	public static final String IN_BRTC = "inBrtc";
	public static final String IN_BZ = "inBz";
	public static final String IN_ZSQK = "inZsqk";
	public static final String IN_HJQK = "inHjqk";
	public static final String IN_XKKM = "inXkkm";
	public static final String IN_XXBZ1 = "inXxbz1";
	public static final String IN_XXBZ2 = "inXxbz2";
	public static final String IN_XXBZ3 = "inXxbz3";
	public static final String IN_ZYDM01 = "inZydm01";
	public static final String IN_ZYMC01 = "inZymc01";
	public static final String IN_ZYDM02 = "inZydm02";
	public static final String IN_ZYMC02 = "inZymc02";
	public static final String IN_ZYDM03 = "inZydm03";
	public static final String IN_ZYMC03 = "inZymc03";
	public static final String IN_ZYDM04 = "inZydm04";
	public static final String IN_ZYMC04 = "inZymc04";
	public static final String IN_ZYDM05 = "inZydm05";
	public static final String IN_ZYMC05 = "inZymc05";
	public static final String IN_ZYDM06 = "inZydm06";
	public static final String IN_ZYMC06 = "inZymc06";
	public static final String IN_ZYDM07 = "inZydm07";
	public static final String IN_ZYMC07 = "inZymc07";
	public static final String IN_ZYDM08 = "inZydm08";
	public static final String IN_ZYMC08 = "inZymc08";
	public static final String IN_ZYDM09 = "inZydm09";
	public static final String IN_ZYMC09 = "inZymc09";
	public static final String IN_ZYDM10 = "inZydm10";
	public static final String IN_ZYMC10 = "inZymc10";
	public static final String IN_ZYFC = "inZyfc";
	public static final String IN_YWDD = "inYwdd";
	public static final String IN_SXDD = "inSxdd";
	public static final String IN_WYDD = "inWydd";
	public static final String IN_ZZDD = "inZzdd";
	public static final String IN_LSDD = "inLsdd";
	public static final String IN_DLDD = "inDldd";
	public static final String IN_WLDD = "inWldd";
	public static final String IN_HXDD = "inHxdd";
	public static final String IN_SWDD = "inSwdd";
	public static final String IN_JSDD = "inJsdd";
	public static final String IN_XXDD = "inXxdd";
	public static final String IN_TYDD = "inTydd";
	public static final String IN_YKXKKM = "inYkxkkm";
	public static final String IN_PDBX = "inPdbx";
	public static final String IN_YDJK = "inYdjk";
	public static final String IN_YSSY = "inYssy";
	public static final String IN_CXSJ = "inCxsj";

	protected void initDao() {
		// do nothing
	}

	public void sort(Criteria criteria, String sort, String order) {
		if (sort!=null && order!=null) {
			if (order.equals("asc")) {
				criteria.addOrder(Order.asc(sort));
			} else {
				criteria.addOrder(Order.desc(sort));
			}
		} else {
			criteria.addOrder(Order.desc(IN_ID));			
		}
	}
	
	public void save(ZsUserinfo transientInstance) {
		log.debug("saving ZsUserinfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void update(ZsUserinfo transientInstance) {
		log.debug("updating ZsUserinfo instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	
	public void delete(ZsUserinfo persistentInstance) {
		log.debug("deleting ZsUserinfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ZsUserinfo findById(java.lang.String id) {
		log.debug("getting ZsUserinfo instance with id: " + id);
		try {
			ZsUserinfo instance = (ZsUserinfo) getHibernateTemplate().get(
					"com.zjut.zs.userinfo.dao.ZsUserinfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ZsUserinfo instance) {
		log.debug("finding ZsUserinfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ZsUserinfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ZsUserinfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByInPinying(Object inPinying) {
		return findByProperty(IN_PINYING, inPinying);
	}

	public List findByInPhoto(Object inPhoto) {
		return findByProperty(IN_PHOTO, inPhoto);
	}

	public List findByInByxxmc(Object inByxxmc) {
		return findByProperty(IN_BYXXMC, inByxxmc);
	}

	public List findByInBylb(Object inBylb) {
		return findByProperty(IN_BYLB, inBylb);
	}

	public List findByInXb(Object inXb) {
		return findByProperty(IN_XB, inXb);
	}

	public List findByInMz(Object inMz) {
		return findByProperty(IN_MZ, inMz);
	}

	public List findByInKslb(Object inKslb) {
		return findByProperty(IN_KSLB, inKslb);
	}

	public List findByInZzmm(Object inZzmm) {
		return findByProperty(IN_ZZMM, inZzmm);
	}

	public List findByInZjlx(Object inZjlx) {
		return findByProperty(IN_ZJLX, inZjlx);
	}

	public List findByInZjhm(Object inZjhm) {
		return findByProperty(IN_ZJHM, inZjhm);
	}

	public List findByInHjmc(Object inHjmc) {
		return findByProperty(IN_HJMC, inHjmc);
	}

	public List findByInLxdh(Object inLxdh) {
		return findByProperty(IN_LXDH, inLxdh);
	}

	public List findByInLxdh2(Object inLxdh2) {
		return findByProperty(IN_LXDH2, inLxdh2);
	}

	public List findByInKl(Object inKl) {
		return findByProperty(IN_KL, inKl);
	}

	public List findByInWyyz(Object inWyyz) {
		return findByProperty(IN_WYYZ, inWyyz);
	}

	public List findByInSdmc(Object inSdmc) {
		return findByProperty(IN_SDMC, inSdmc);
	}

	public List findByInSxmc(Object inSxmc) {
		return findByProperty(IN_SXMC, inSxmc);
	}

	public List findByInBmdmc(Object inBmdmc) {
		return findByProperty(IN_BMDMC, inBmdmc);
	}

	public List findByInBrtc(Object inBrtc) {
		return findByProperty(IN_BRTC, inBrtc);
	}

	public List findByInBz(Object inBz) {
		return findByProperty(IN_BZ, inBz);
	}

	public List findByInZsqk(Object inZsqk) {
		return findByProperty(IN_ZSQK, inZsqk);
	}

	public List findByInHjqk(Object inHjqk) {
		return findByProperty(IN_HJQK, inHjqk);
	}

	public List findByInXkkm(Object inXkkm) {
		return findByProperty(IN_XKKM, inXkkm);
	}

	public List findByInXxbz1(Object inXxbz1) {
		return findByProperty(IN_XXBZ1, inXxbz1);
	}

	public List findByInXxbz2(Object inXxbz2) {
		return findByProperty(IN_XXBZ2, inXxbz2);
	}

	public List findByInXxbz3(Object inXxbz3) {
		return findByProperty(IN_XXBZ3, inXxbz3);
	}

	public List findByInZydm01(Object inZydm01) {
		return findByProperty(IN_ZYDM01, inZydm01);
	}

	public List findByInZymc01(Object inZymc01) {
		return findByProperty(IN_ZYMC01, inZymc01);
	}

	public List findByInZydm02(Object inZydm02) {
		return findByProperty(IN_ZYDM02, inZydm02);
	}

	public List findByInZymc02(Object inZymc02) {
		return findByProperty(IN_ZYMC02, inZymc02);
	}

	public List findByInZydm03(Object inZydm03) {
		return findByProperty(IN_ZYDM03, inZydm03);
	}

	public List findByInZymc03(Object inZymc03) {
		return findByProperty(IN_ZYMC03, inZymc03);
	}

	public List findByInZydm04(Object inZydm04) {
		return findByProperty(IN_ZYDM04, inZydm04);
	}

	public List findByInZymc04(Object inZymc04) {
		return findByProperty(IN_ZYMC04, inZymc04);
	}

	public List findByInZydm05(Object inZydm05) {
		return findByProperty(IN_ZYDM05, inZydm05);
	}

	public List findByInZymc05(Object inZymc05) {
		return findByProperty(IN_ZYMC05, inZymc05);
	}

	public List findByInZydm06(Object inZydm06) {
		return findByProperty(IN_ZYDM06, inZydm06);
	}

	public List findByInZymc06(Object inZymc06) {
		return findByProperty(IN_ZYMC06, inZymc06);
	}

	public List findByInZydm07(Object inZydm07) {
		return findByProperty(IN_ZYDM07, inZydm07);
	}

	public List findByInZymc07(Object inZymc07) {
		return findByProperty(IN_ZYMC07, inZymc07);
	}

	public List findByInZydm08(Object inZydm08) {
		return findByProperty(IN_ZYDM08, inZydm08);
	}

	public List findByInZymc08(Object inZymc08) {
		return findByProperty(IN_ZYMC08, inZymc08);
	}

	public List findByInZydm09(Object inZydm09) {
		return findByProperty(IN_ZYDM09, inZydm09);
	}

	public List findByInZymc09(Object inZymc09) {
		return findByProperty(IN_ZYMC09, inZymc09);
	}

	public List findByInZydm10(Object inZydm10) {
		return findByProperty(IN_ZYDM10, inZydm10);
	}

	public List findByInZymc10(Object inZymc10) {
		return findByProperty(IN_ZYMC10, inZymc10);
	}

	public List findByInZyfc(Object inZyfc) {
		return findByProperty(IN_ZYFC, inZyfc);
	}

	public List findByInYwdd(Object inYwdd) {
		return findByProperty(IN_YWDD, inYwdd);
	}

	public List findByInSxdd(Object inSxdd) {
		return findByProperty(IN_SXDD, inSxdd);
	}

	public List findByInWydd(Object inWydd) {
		return findByProperty(IN_WYDD, inWydd);
	}

	public List findByInZzdd(Object inZzdd) {
		return findByProperty(IN_ZZDD, inZzdd);
	}

	public List findByInLsdd(Object inLsdd) {
		return findByProperty(IN_LSDD, inLsdd);
	}

	public List findByInDldd(Object inDldd) {
		return findByProperty(IN_DLDD, inDldd);
	}

	public List findByInWldd(Object inWldd) {
		return findByProperty(IN_WLDD, inWldd);
	}

	public List findByInHxdd(Object inHxdd) {
		return findByProperty(IN_HXDD, inHxdd);
	}

	public List findByInSwdd(Object inSwdd) {
		return findByProperty(IN_SWDD, inSwdd);
	}

	public List findByInJsdd(Object inJsdd) {
		return findByProperty(IN_JSDD, inJsdd);
	}

	public List findByInXxdd(Object inXxdd) {
		return findByProperty(IN_XXDD, inXxdd);
	}

	public List findByInTydd(Object inTydd) {
		return findByProperty(IN_TYDD, inTydd);
	}

	public List findByInYkxkkm(Object inYkxkkm) {
		return findByProperty(IN_YKXKKM, inYkxkkm);
	}

	public List findByInPdbx(Object inPdbx) {
		return findByProperty(IN_PDBX, inPdbx);
	}

	public List findByInYdjk(Object inYdjk) {
		return findByProperty(IN_YDJK, inYdjk);
	}

	public List findByInYssy(Object inYssy) {
		return findByProperty(IN_YSSY, inYssy);
	}

	public List findByInCxsj(Object inCxsj) {
		return findByProperty(IN_CXSJ, inCxsj);
	}

	public List findAll() {
		log.debug("finding all ZsUserinfo instances");
		try {
			String queryString = "from ZsUserinfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ZsUserinfo merge(ZsUserinfo detachedInstance) {
		log.debug("merging ZsUserinfo instance");
		try {
			ZsUserinfo result = (ZsUserinfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ZsUserinfo instance) {
		log.debug("attaching dirty ZsUserinfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ZsUserinfo instance) {
		log.debug("attaching clean ZsUserinfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ZsUserinfoDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ZsUserinfoDAO) ctx.getBean("ZsUserinfoDAO");
	}

	@SuppressWarnings("unchecked")
	public PageModule findByInstance(final int pageNumber ,final int pageSize, String sort, String order, final ZsUserinfo obj){
		try {
			int startIndex = (pageNumber - 1) * pageSize;
			Session session = getHibernateTemplate().getSessionFactory().openSession();
			List list;
			Criteria criteria = session.createCriteria(ZsUserinfo.class);
			
			if (null != obj.getZsUser()) {
				criteria.add(Restrictions.eq("zsUser", obj.getZsUser()));
			}
			//补充
			
			this.sort(criteria, sort, order);
			
			criteria.setFirstResult(startIndex);
			criteria.setMaxResults(pageSize);
			list = criteria.list();
			session.close();
			return new PageModule(pageNumber, pageSize, countByInstance(obj), list);
		} catch (RuntimeException re) {
			log.error("findByInstance failed", re);
			throw re;
		}
	}
	
	public int countByInstance(final ZsUserinfo obj) {
		log.debug("finding the number of ZsUserinfo instance");
		try {
			int count = 0;
			Session session = getHibernateTemplate().getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(ZsUserinfo.class);
			
			if (null != obj.getZsUser()) {
				criteria.add(Restrictions.eq("zsUser", obj.getZsUser()));
			}
			//补充
			
			count = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
			session.close();
			return count;
		} catch (RuntimeException re) {
			log.error("find the number of ZsUserinfo instance failed", re);
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List findByHql(final int pageNumber ,final int pageSize, final String hql, final Object[] values){
		log.debug("finding ZsUserinfo instance by hql");
	    return (List<ZsUserinfo>) super.getHibernateTemplate().execute(new HibernateCallback(){
	        public Object doInHibernate(Session session) throws HibernateException, SQLException {
	            Query query=session.createQuery(hql);
	            if (values != null) {// 为hql语句传入参数
					for (int i = 0; i < values.length; i++) {
						query.setParameter(i, values[i]);
					}
				}
	            return query.setFirstResult((pageNumber-1)*pageSize).setMaxResults(pageSize).list();
	        }
	    });
	}

	@SuppressWarnings("unchecked")
	public int findCountByHql(final String hql, final Object[] values) {
		log.debug("finding the number of ZsUserinfo instance");
		try {
			return (Integer) super.getHibernateTemplate().execute(new HibernateCallback(){
		        public Object doInHibernate(Session session) throws HibernateException, SQLException {
		            Query query=session.createQuery(hql);
		            if (values != null) {// 为hql语句传入参数
						for (int i = 0; i < values.length; i++) {
							query.setParameter(i, values[i]);
						}
					}
					return ((Long)query.uniqueResult()).intValue();
		        }
		    });
		} catch (RuntimeException re) {
			log.error("find the number of ZsUserinfo instance failed", re);
			throw re;
		}
	}
	
	private boolean isEmpty(String value) {
		if (value==null || value.trim().length()==0) {
			return true;
		}
		return false;
	}
}